package chpt01;

import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.GregorianCalendar;
import java.util.Scanner;


class Student {
		
		public Student(){
			
		}
		public Student(int id, String name, int age, double grade) {
			this.id = id;
			this.setName(name);
			this.setAge(age);
			this.setGrade(grade);
		}
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			if (name.length()>10){
				throw new IllegalArgumentException("name's length should <=10 "+name.length());
			}
			this.name = name;
		}
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			if (age<=0){
				throw new IllegalArgumentException("age should >0 "+age);
			}
			this.age = age;
		}
		public double getGrade() {
			return grade;
		}
		public void setGrade(double grade) {
			if (grade<0 || grade >100){
				throw new IllegalArgumentException("grade should be in [0,100] "+grade);
			}
			this.grade = grade;
		}
		@Override
		public String toString() {
			return getClass().getName()+" [id=" + id + ", name=" + name + ", age=" + age + ", grade=" + grade + "]";
		}
		public void writeData(PrintWriter out) {
			// TODO Auto-generated method stub
			//GregorianCalendar calendar = new GregorianCalendar();
		    //calendar.setTime(hireDay);
		    out.println(id + "|" + name + "|" + age +"|"+grade);
		}	
		public void readData(Scanner in)
		   {
		      String line = in.nextLine();
		      String[] tokens = line.split("\\|");
		      id = Integer.parseInt(tokens[0]);
		      name = tokens[1];
		      age = Integer.parseInt(tokens[2]);
		      grade = Double.parseDouble(tokens[3]);
	
	   }
		private int id;
		private String name;
		private int age;
		private double grade;
		public static class mian {

			public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student[] stu = new Student[3];

	    stu[0] = new Student(44, "Carl",18,2);
	    stu[1] = new Student(35,"Harry",20,1);
	    stu[2] = new Student(46,"Tony", 21,4);
	    //201521123030
		try
	    {
	       // save all employee records to the file employee.dat
	       PrintWriter out = new PrintWriter("d:/student.txt");
	       writeData(stu, out);
	       out.close();

	       // retrieve all records into a new array
	       Scanner in = new Scanner(new FileReader("d:/student.txt"));
	       Student[]  newStudent = readData(in);
	      // in.close();
	     
	       // print the newly read employee records
	       for (Student e : newStudent)
	          System.out.println(e);
	    }
	    catch (IOException exception)
	    {
	       exception.printStackTrace();
	    }
	 }
	private static void writeData(Student[] students, PrintWriter out) throws IOException {
		// TODO Auto-generated method stub
		out.println(students.length);
		for (Student e : students)
	        e.writeData(out);
	}


	private static Student[] readData(Scanner in) {
		// TODO Auto-generated method stub
		int n = in.nextInt();
	    in.nextLine(); // consume newline

	    Student[] students = new Student[n];
	    for (int i = 0; i < n; i++)
	    {
	    	students[i] = new Student();
	    	students[i].readData(in);
	    }
	    return students;
	}
}
